package com.app.web.order.job;

import com.alibaba.fastjson.JSON;
import com.app.utils.base.j2se.DateUtil;
import com.app.utils.base.j2se.Logger;
import com.app.utils.redis.Redis;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description  收益结算
 * Created by 王伟
 * Date create on 15:28 2019/12/12.
 */
@Component
public class UserMoneyWithdrawal implements Job {
    @Autowired
    private OrderAnalyService orderAnalyService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("*****begin*****"+ DateUtil.getDatetime()+"-分析处理用户可提现金额结束-UserMoneyWithdrawal");
        Map<String, Object> paramMap = new HashMap<>();
        try{
            Redis.str.set("withdrawal_expect","true",10000);
            //获取总共已结算的收益
            StringBuilder bday = new StringBuilder();
            if(Integer.parseInt(DateUtil.getMonth(0))!=1){
                bday.append(DateUtil.getYear(0));
            }else{
                bday.append(DateUtil.getYear(-1));
            }
            bday.append(DateUtil.getMonth(-1));
            bday.append("16");
            String eday = DateUtil.getYear(0) +
                    DateUtil.getMonth(0) +
                    "15";
            paramMap.put("bday", Integer.parseInt(bday.toString()));
            paramMap.put("eday",Integer.parseInt(eday));
            System.out.println("分析处理用户可提现金额开始查询数据");
            List<Map<String, Object>> dataList = orderAnalyService.getUserMoneyWithdrawalExpect(paramMap);
            System.out.println("count=" + dataList.size());
            //更新用户金额 记录明细等一系列操作
            int succ = orderAnalyService.updateOverOrderMoney(dataList);
            System.out.println("分析处理用户PDD可提现金额本次执行成功条数："+succ);
            System.out.println();
        }catch (Exception e){
            Map<String,Object> map = new HashMap<>();
            map.put("module",this.getClass().getName());
            map.put("methods",Thread.currentThread().getStackTrace()[1].getMethodName());
            map.put("desc","分析处理用户成功收益");
            map.put("json",JSON.toJSONString(paramMap));
//            map.put("exce", Logger.exceptionToString(e));
            /* TODO 发送钉钉推送 */
//            logService.taskLog(map);
            Logger.error(String.format("分析处理用户可提现金额结束异常，param:%s", JSON.toJSONString(map)),e);
            throw new RuntimeException("分析处理用户失效收益");
        }finally {
            /* 结算结束 */
            Redis.key.del("withdrawal_expect");
        }
        System.out.println("*****end*****"+ DateUtil.getDatetime()+"-分析处理用户可提现金额结束-UserMoneyWithdrawal");
        System.out.println();
    }

}
